package inventoryservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"

	"github.com/zeromicro/go-zero/core/logx"
)

type InventoryAssetReturnAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInventoryAssetReturnAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InventoryAssetReturnAddLogic {
	return &InventoryAssetReturnAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// InventoryAssetReturnAdd 创建资产退库表
func (l *InventoryAssetReturnAddLogic) InventoryAssetReturnAdd(in *property_service.InventoryAssetReturn) (*property_service.ResponseId, error) {
	// 获取资产退库表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).InventoryAssetReturn
	// 创建一个新的资产退库表模型对象，不包含ID、UpdatedAt、CreatedAt字段
	mod := &model.InventoryAssetReturn{
		ReturnCode:       in.ReturnCode,                          // 退库单号
		ReturnDate:       BeanUtils.GetDateFormat(in.ReturnDate), // 退库日期
		Warehouse:        in.Warehouse,                           // 仓库
		Preparer:         in.Preparer,                            // 制单人
		WarehouseManager: in.WarehouseManager,                    // 仓管员
		ReturnDept:       in.ReturnDept,                          // 退库科室
		Returner:         in.Returner,                            // 退库人
		Remarks:          in.Remarks,                             // 备注
		StatusID:         in.StatusID,                            // 状态ID
		ReturnStatus:     in.ReturnStatus,                        // 状态
		StockID:          in.StockID,                             // 仓库ID
		TenantID:         in.TenantID,                            // 租户ID
		CreatedAtuser:    in.CreatedAtuser,                       // 创建用户
	}

	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		res, err := result.Err(err).Ed()
		return l.buildResponse(mod.ID, res, err)
	}
	// 创建成功，返回成功响应
	res, err := result.Suc().AddEd()
	return l.buildResponse(mod.ID, res, err)
}

// 在 InventoryAssetReturnAddLogic 结构体中添加辅助方法
func (l *InventoryAssetReturnAddLogic) buildResponse(id int64, res *property_service.Response, err error) (*property_service.ResponseId, error) {
	return &property_service.ResponseId{
		Id:      id,
		Code:    res.Code,
		Message: res.Message,
	}, err
}
